<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/17
 * Time: 11:05
 */
namespace app\home\model;
use \think\Model;
use \think\Db;
class UserinfoModel extends Model
{
    //修改用户消息
    public function saveUserInfo($id, $data)
    {
        $res = Db::table('users')->where("id=$id")->update($data);
        return $res;
    }

    //读取用户信息
    public function find($table,$condition='')
    {
        $row = Db::table($table)->where($condition)->find();
        return $row;
    }

    //选择所有消息
    public function selectMessage($id)
    {
        $rows = Db::table('messages')->where("user_id=$id")->order('create_time desc')->select();
        return $rows;
    }

    //选择未读消息
    public function selectNotRead($id)
    {
        $rows = Db::table('messages')->where("user_id=$id and status=0")->select();
        return $rows;
    }

    //读取消息后更新状态
    public function updateMessage($id, $data)
    {
        $rows = Db::table('messages')->where("user_id=$id")->update($data);
        return $rows;
    }

    //选取数据
    public function select($table,$condition='')
    {
        $rows = Db::table($table)->where($condition)->select();
        return $rows;
    }
    //查看我的未使用优惠券
    public function selectCoupons($condition='')
    {
        $rows = Db::table('coupons_users')->where($condition)->join('coupons','coupons.id =coupons_users.coupon_id')->order('receive_time desc')->select();
        return $rows;
    }
    //删除优惠券
    public function delCoupon($condition='')
    {
        $res = Db::table('coupons_users')->where($condition)->delete();
        return $res;
    }
    //增加
    public function insert($table,$data){
        $res = Db::table($table)->insert($data);
        return $res;
    }
    //修改
    public function saveA($table,$condition='',$data){
        $res = Db::table($table)->where($condition)->update($data);
        return $res;
    }
    //删除
    public function deleteOne($table,$condition){
        $res  =Db::table($table)->where($condition)->delete();
        return $res;
    }
    //地区联合查询
    public function selectAreas($id)
    {
        $rows = Db::table('areas_user')
            ->join('areas','areas.areaId =areas_user.area_id')
            ->join('user_address','user_address.user_id =areas_user.user_id')
            ->where("areas_user.user_id=$id")
            ->order('create_time desc')
            ->limit(3)
            ->select();
//        echo '<pre>';
//        print_r($rows);
//        exit;
        return $rows;
    }
    //获取订单状态
    public function orderNum($order_user_id){
        $rows=[];
        $rows['wait_payment'] = Db::table('orders')->where("user_id =$order_user_id and is_del=0 and order_status = 0")->count();//待付款
        $rows['wait_send'] = Db::table('orders')->where("user_id =$order_user_id and order_status =1")->count();//待发货
        $rows['wait_receipt'] = Db::table('orders')->where("user_id =$order_user_id and order_status =2")->count();//待收货
        $rows['wait_appraise'] = Db::table('orders')->where("user_id =$order_user_id and order_status =3 and is_appraises =0")->count();//待评价
        $rows['wait_return'] = Db::table('orders')->where("user_id =$order_user_id and order_status = 6")->count();//待退货
        return $rows;
    }
}